package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn30100Bo;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang 车辆修改结果controller
 */
public class CarUpdateResultController extends TmsController {

	private Logger logger = Logger.getLogger(CarUpdateResultController.class);

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Car car = Car.getInstance();
			CarCreationInputViewHelper viewHelper = new CarCreationInputViewHelper();
			viewHelper = (CarCreationInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			car = setEntityFromViewHelper(viewHelper, car);
			TmsUserProfile profile = uc.getUserProfile();
			car.setUpdateUserId(profile.getUniqueId());

			// 上传图片部分，判断某部位是否有图片 图片保存在图片表中
			List<CarImage> list = (List<CarImage>) uc.getAttribute("imgList");
			// 如果是待入库的车辆，要取得对应的id值
			if (uc.getAttribute("id") != null) {
				String id = (String) uc.getAttribute("id");
				// 新录入车辆是在待入库部分（需要给录入完的车辆的状态进行修改）
				car.setInnerType(2);
				car.setId(Long.parseLong(id));
			}
			if (list == null) {
				list = new ArrayList<CarImage>();
			}
			if (viewHelper.getImg1() != null
					&& !"".equals(viewHelper.getImg1())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("1");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg1());
				list.add(entity);
			}
			if (viewHelper.getImg2() != null
					&& !"".equals(viewHelper.getImg2())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("2");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg2());
				list.add(entity);
			}
			if (viewHelper.getImg3() != null
					&& !"".equals(viewHelper.getImg3())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("3");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg3());
				list.add(entity);
			}
			if (viewHelper.getImg4() != null
					&& !"".equals(viewHelper.getImg4())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("4");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg4());
				list.add(entity);
			}
			if (viewHelper.getImg5() != null
					&& !"".equals(viewHelper.getImg5())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("5");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg5());
				list.add(entity);
			}
			if (viewHelper.getImg6() != null
					&& !"".equals(viewHelper.getImg6())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("6");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg6());
				list.add(entity);
			}
			if (viewHelper.getImg7() != null
					&& !"".equals(viewHelper.getImg7())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("7");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg7());
				list.add(entity);
			}
			if (viewHelper.getImg8() != null
					&& !"".equals(viewHelper.getImg8())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("8");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg8());
				list.add(entity);
			}
			String sysPath = (String) uc.getAttribute("sysPath");
			
			//修改
			car.updateCar(car, list, sysPath);
			//存入过户信息表
			TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();

			transferAcceptedInfo.setUpdateUserId(profile.getUniqueId());
			transferAcceptedInfo.setSerial(viewHelper.getTransferSerial());
			transferAcceptedInfo.setCarOwner(viewHelper.getCarOwner());
			transferAcceptedInfo.setCarOwnerIdCardNo(viewHelper.getCarOwnerIdCardNo());
			transferAcceptedInfo.setOwnerPhone(viewHelper.getOwnerPhone());
			transferAcceptedInfo.setOwnerAddress(viewHelper.getOwnerAddress());
			transferAcceptedInfo.setOwnerOrgNo(viewHelper.getOwnerOrgNo());
			transferAcceptedInfo.setOwnerType(Integer.parseInt(viewHelper.getOwnerType()));
			transferAcceptedInfo.setCy_cardType(viewHelper.getCardType());
			transferAcceptedInfo.updateAcceptedInfoBySerial(transferAcceptedInfo);
			uc.removeAttribute("imgList");
			controller.commit();
			
			//
			car = car.findCarBySerial(car.getSerial());
			MobileMessage message = new MobileMessage();
			message.putValue("Serial", car.getSerial());
			message.setMessage("【TMS】新增入库（库存存在情况的更新保存）【车代办】同步,Serial为#Serial#");
			TransferDataSyn30100Bo api = new TransferDataSyn30100Bo();
			api.setMobileMessage(message);
			api.setSerial(car.getSerial());
			api.setLicensePlate(car.getLicensePlate());
			api.setVin(car.getVin());
			api.setMileage(car.getMileage());
			api.setBrandId(car.getBrandId());
			api.setVendorId(car.getVendorId());
			api.setCarStyleId(car.getCarStyleId());
			api.setColor(car.getColor());
			api.setMemberId(StringUtil.getString(car.getMemberId()));
			api.setPrice(car.getPrice());
			api.setRegDate(car.getRegDate());
			api.setJqx(car.getJqx());
			api.setYearCarefulPeriod(car.getYearCarefulPeriod());
			api.setContactPerson(String.valueOf(car.getCompanyContactPerson()));
			api.setContactPhone(car.getCompanyContactPhone());
			api.setRemark(String.valueOf(car.getAttribute()));
			api.setPhoto1(car.getPhoto_1());
			api.setPhoto2(car.getPhoto_2());
			api.setPhoto3(car.getPhoto_3());
			api.setPhoto4(car.getPhoto_4());
			api.setPhoto5(car.getPhoto_5());
			api.setPhoto6(car.getPhoto_6());
			api.setPhoto7(car.getPhoto_7());
			api.setPhoto8(car.getPhoto_8());
			api.setIsMatch(car.getIsMatch());
			api.setRegisterStatus(car.getRegisterStatus());
			api.setCarName(car.getCarName());
			api.setCatClass(car.getCatClass());
			api.setIsShelve(car.getIsShelve());
			api.setTempLicensePlate(car.getTempLicensePlate());
			PullingExecutor.getInstance().submit(api);
			//
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 把viewHelper的值赋给Entity
	 * 
	 * @param viewHelper
	 * @param car
	 * @return
	 * @throws TmsDaoException
	 */
	private Car setEntityFromViewHelper(CarCreationInputViewHelper viewhelper,Car car) throws TmsDaoException {
		car.setId(viewhelper.getId());
		if (!StringUtil.isNullOrBlank(viewhelper.getAttribute()))  car.setAttribute(Integer.parseInt(viewhelper.getAttribute()));
		car.setSerial(viewhelper.getSerial());
		car.setLicensePlate(viewhelper.getLicensePlate());
		car.setRegisterStatus(viewhelper.getRegisterStatus());
		car.setBrandId(viewhelper.getBrandId());
		car.setCarStyleId(viewhelper.getCarStyleId());
		car.setVendorId(viewhelper.getVendorId());
		if ("".equals(viewhelper.getMileage())) car.setMileage(0);
		else car.setMileage(Integer.parseInt(StringUtil.removeDH(viewhelper.getMileage())));
		if ("".equals(viewhelper.getPrice())) car.setPrice(0);
		else car.setPrice(Integer.parseInt(StringUtil.removeDH(viewhelper.getPrice())));
		if (!StringUtil.isNullOrBlank(viewhelper.getColor())) car.setColor(Integer.parseInt(viewhelper.getColor()));
		car.setRegDate(viewhelper.getRegDate());
		car.setJqx(viewhelper.getJqx());
		car.setYearCarefulPeriod(viewhelper.getYearCarefulPeriod());
		car.setCompanyContactPerson(viewhelper.getCompanyContactPerson());
		car.setCompanyContactPhone(viewhelper.getCompanyContactPhone());
		car.setRemark(viewhelper.getRemark());
		car.setPhoto_1(viewhelper.getImg1());
		car.setPhoto_2(viewhelper.getImg2());
		car.setPhoto_3(viewhelper.getImg3());
		car.setPhoto_4(viewhelper.getImg4());
		car.setPhoto_5(viewhelper.getImg5());
		car.setPhoto_6(viewhelper.getImg6());
		car.setPhoto_7(viewhelper.getImg7());
		car.setPhoto_8(viewhelper.getImg8());
		
		
		
//		Codes code = Codes.getInstance();
//		car.setId(viewhelper.getId());
//		//add by huangheli at 20150816 start
//		if(!StringUtil.isNullOrBlank(viewhelper.getAttribute())){
//			//add by huangheli at 20150816 end
//			car.setAttribute(Integer.parseInt(viewhelper.getAttribute()));
//		}
//		car.setBrandId(viewhelper.getBrandId());
//		if(!StringUtil.isNullOrBlank(viewhelper.getCardType())) car.setCardType(code.getItemCodeByItemIdAndItemValue(Codes.PLATE_TYPE,viewhelper.getCardType()));
//		
//		car.setLicensePlate(viewhelper.getLicensePlate());
//		car.setCarOwner(viewhelper.getCarOwner());
//		car.setCarOwnerIdCardNo(viewhelper.getCarOwnerIdCardNo());
//	
//		if(!StringUtil.isNullOrBlank(viewhelper.getCarType())) car.setCarType(Integer.parseInt(viewhelper.getCarType()));
//		if(!StringUtil.isNullOrBlank(viewhelper.getCarUse())) car.setCarUse(Integer.parseInt(viewhelper.getCarUse()));
//		if(!StringUtil.isNullOrBlank(viewhelper.getColor())) car.setColor(Integer.parseInt(viewhelper.getColor()));
//		car.setCompanyContactPerson(viewhelper.getCompanyContactPerson());
//		car.setCompanyContactPhone(viewhelper.getCompanyContactPhone());
//		if(!StringUtil.isNullOrBlank(viewhelper.getCountryStyle()))  car.setCountryStyle(Integer.parseInt( viewhelper.getCountryStyle()));
//		if ("".equals(viewhelper.getDisplacement())) {
//			car.setDisplacement(0);
//		} else {
//			car.setDisplacement(Integer.parseInt(viewhelper.getDisplacement()));
//		}
//
//		car.setEngine(viewhelper.getEngine());
//		if(!StringUtil.isNullOrBlank(viewhelper.getFuelType()))  car.setFuelType(Integer.parseInt(viewhelper.getFuelType()));
//		if(!StringUtil.isNullOrBlank(viewhelper.getGearbox())) car.setGearbox(Integer.parseInt(viewhelper.getGearbox()));
//		car.setJqx(viewhelper.getJqx());
//		if (!StringUtil.isNullOrBlank(viewhelper.getLoadquality())) {
//			car.setLoadquality(Integer.parseInt(viewhelper.getLoadquality()));
//		} else {
//			car.setLoadquality(0);
//		}
//		
//		car.setBrandId(viewhelper.getBrandId());
//		car.setVendorId(viewhelper.getVendorId());
//		car.setCarName(viewhelper.getCarName());
//		
//		car.setCarStyleId(viewhelper.getCarStyleId());
//		Member member = Member.getInstance();
//		if("1".equals(viewhelper.getSearchFlag())){
//			car.setMemberId(Integer.parseInt(viewhelper.getMemberId()));
//		}else{
//			car.setMemberId(member.getMemIdByMemName(viewhelper.getMemberId()));
//		}
//		
//		if ("".equals(viewhelper.getMileage())) {
//			car.setMileage(0);
//		} else {
//			car.setMileage(Integer.parseInt(StringUtil.removeDH(viewhelper.getMileage())));
//		}
//
//		car.setModelCode(viewhelper.getModelCode());
//		car.setOwnerAddress(viewhelper.getOwnerAddress());
//		car.setOwnerCardNo(viewhelper.getOwnerCardNo());
//		car.setOwnerOrgNo(viewhelper.getOwnerOrgNo());
//		car.setOwnerPhone(viewhelper.getOwnerPhone());
//		if(viewhelper.getOwnerTypeFlag() == 1){
//			car.setOwnerType(Integer.parseInt(viewhelper.getOwnerType()));
//		}else{
//			car.setOwnerType(Integer.parseInt(viewhelper.getOwnerType()));
//		}
//		//add by huangheli at 20150816 start
//		if(!StringUtil.isNullOrBlank(viewhelper.getPassenger())){
//		//add by huangheli at 20150816 end
//		car.setPassenger(Integer.parseInt(viewhelper.getPassenger()));
//		}
//		if ("".equals(viewhelper.getPrice())) {
//			car.setPrice(0);
//		} else {
//			car.setPrice(Integer.parseInt(StringUtil.removeDH(viewhelper.getPrice())));
//		}
//
//		car.setRegDate(viewhelper.getRegDate());
//		car.setRegisterCardNo(viewhelper.getRegisterCardNo());
//		car.setSendCardDate(viewhelper.getSendCardDate());
//		car.setSerial(viewhelper.getSerial());
//		
//		car.setVendorCode(viewhelper.getVendorCode());
//		car.setVin(viewhelper.getVin());
//		car.setYearCarefulPeriod(viewhelper.getYearCarefulPeriod());
//		car.setRegisterStatus(viewhelper.getRegisterStatus());
//		if(viewhelper.getStatus() == 5){
//			car.setStatus(3);
//		}else{
//			car.setStatus(viewhelper.getStatus());
//		}
//		
//		car.setAttestStatus(viewhelper.getAttestStatus());
//		car.setEntryStatus(4);
//		car.setPhoto_1(viewhelper.getImg1());
//		car.setPhoto_2(viewhelper.getImg2());
//		car.setPhoto_3(viewhelper.getImg3());
//		car.setPhoto_4(viewhelper.getImg4());
//		car.setPhoto_5(viewhelper.getImg5());
//		car.setPhoto_6(viewhelper.getImg6());
//		car.setPhoto_7(viewhelper.getImg7());
//		car.setPhoto_8(viewhelper.getImg8());
		
		return car;
	}
}
